package com.meishij;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.meishij.adapter.SplashActivityViewPagerAdapter;
import com.meishij.entry.RecommendEntry;

import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends AppCompatActivity implements Handler.Callback {

    private Handler handler = new Handler(this);
    private ViewPager viewPager;
    private List<String> urlList;
    private static final int SHOW_TIME_MIN = 2000;// 最小显示时间
    private long mStartTime;// 开始时间
    private RecommendEntry responseData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        setContentView(R.layout.activity_splash);
        urlList = new ArrayList<>();
        urlList.add("http://site.meishij.net/adm/additive/2016-09-09/57d288266572f.jpg");
        urlList.add("http://site.meishij.net/app/open/jiuyang0914_750x1336.jpg");
        viewPager = ((ViewPager) findViewById(R.id.splashActivity_viewpager));
        viewPager.setAdapter(new SplashActivityViewPagerAdapter(urlList));
        Message msg = new Message();
        msg.what = 0;
        handler.sendMessageDelayed(msg, 1000);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                viewPager.setCurrentItem(1, false);

                mStartTime = System.currentTimeMillis();
                initdata(handler);
//                handler.sendMessageDelayed(handler.obtainMessage(1,0,0),4000);
                break;
            case 1:
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setClass(getApplication(), MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.putExtra("responseData",responseData);
                startActivity(intent);
                finish();
//                overridePendingTransition(R.anim.activity_in, R.anim.splash_out);//activity切换时的动画,必须在startActivity或者finish之后调用
                break;
        }
        return false;
    }

    private void initdata(final Handler handler) {

        /*Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl("http://api.meishi.cc/v5/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RecommendServer recommendServer = retrofit.create(RecommendServer.class);
        Call<RecommendEntry> recommendEntry = recommendServer.getRecommendEntry();
        recommendEntry.enqueue(new Callback<RecommendEntry>() {
            @Override
            public void onResponse(Call<RecommendEntry> call, Response<RecommendEntry> response) {
                RecommendEntry body = response.body();
                responseData = body;
                long loadTime = System.currentTimeMillis() - mStartTime;
                if (loadTime < SHOW_TIME_MIN) {
                    handler.sendMessageDelayed(handler.obtainMessage(1, 0, 0), SHOW_TIME_MIN - loadTime);
                } else
                    handler.sendMessage(handler.obtainMessage(1, 0, 0));
            }

            @Override
            public void onFailure(Call<RecommendEntry> call, Throwable t) {

            }
        });*/
        handler.sendMessageDelayed(handler.obtainMessage(1, 0, 0), SHOW_TIME_MIN );
    }
}
